<?php

require_once ('Model.php');

/**
 * 
 */
class News extends Model {

    /**
     *
     * @var type 
     */
    private $id;

    /**
     *
     * @var type 
     */
    private $title;

    /**
     *
     * @var type 
     */
    private $abstract;

    /**
     *
     * @var type 
     */
    private $content;

    /**
     *
     * @var type 
     */
    private $status;

    /**
     *
     * @var type 
     */
    private $created_on;

    /**
     *
     * @var type 
     */
    private $modified_on;

    /**
     * 
     * @param type $id
     * @param type $title
     * @param type $abstract
     * @param type $content
     * @param type $status
     * @param type $created_on
     * @param type $modified_on
     */
    public function __construct($id, $title, $abstract, $content, $status) {

        $this->id = $id;
        $this->title = $title;
        $this->abstract = $abstract;
        $this->content = $content;
        $this->status = $status;
    }

    /**
     * 
     */
    public function Save() {

        if (!get_magic_quotes_gpc()) {
            $this->title = addslashes($this->title);
            $this->content = addslashes($this->content);
            $this->abstract = addslashes($this->abstract);
        }
        $this->created_on = date('Y-m-d H:i:s');
        @ $database = new mysqli('localhost', 'root', '', 'blackyco');
        if (mysqli_connect_errno()) {
            echo '<div class="alert alert-error">';
            echo "Hiba: nem sikerült kapcsolódni az adatbázishoz.";
            echo "</div>";
            exit;
        }
        $insert = "insert into news( title, abstract, content, status, created_on ) values ( '" . $this->title . "', '" . $this->abstract . "', '" . $this->content . "', '" . $this->status . "', '" . $this->created_on . "')";
        $result = $database->query($insert);
        if ($result) {
            echo '<div class="alert alert-success">';
            echo $database->affected_rows . " db hír sikeresen hozzá lett adva az adatbázishoz";
            echo "</div>";
            echo "<hr />";
            echo "<strong>Title:</strong><br />" . $this->title . "<br />";
            echo "<strong>Abstract:</strong><br />" . $this->abstract . "<br />";
            echo "<strong>Content:</strong><br />" . $this->content . "<br />";
            echo "<strong>Created:</strong><br />" . $this->created_on . "<br />";
        } else {
            echo '<div class="alert alert-error">';
            echo "Hiba történt. A művelet sikertelen volt.";
            echo "</div>";
        }
        //$result->free();
        $database->close();
    }

    /**
     * 
     */
    public function Manage() {
        $database = new mysqli('localhost', 'root', '', 'blackyco');
        $query = "select id, title, created_on from news";
        $result = mysqli_query($database, $query);
        echo '<table class="table table-striped">';
        echo '<tr>';
        echo '<th>ID</th>';
        echo '<th>Title</th>';
        echo '<th>Created on</th>';
        echo '</tr>';
        while ($row = mysqli_fetch_array($result)) {
        echo '"<tr><td>" ($row["id"]) . "</td>"';
        echo '"<td>" ($row["title"]) . "</td>"';
        echo '"<td>" ($row["created_on"]) . "</td></tr>\n"';
        }
        echo '</table >'
        mysqli_free_result($result);
        mysqli_close($database)
    }

    /**
     * 
     */
    public function Update() {
        $this->modified_on = date('Y-m-d H:i:s');
        $database = new mysqli('localhost', 'root', '', 'blackyco');
        $update = "update news set title='" . $this->title . "' abstract='" . $this->abstract . "' content='" . $this->content . "' status='" . $this->status . "' where id='" . $this->id . "'";
        $result = $database->query($update);
        $result->free();
        $database->close();
    }

    /**
     * 
     */
    public function Delete() {
        
    }

    /**
     * 
     */
    public function View() {
        
    }

}

?>
